package com.lanou.fooddemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.fooddemo.R;
import com.lanou.fooddemo.activity.TopicDetailsActivity;
import com.lanou.fooddemo.bean.DataBean;
import com.lanou.fooddemo.bean.PicBean;
import com.lanou.fooddemo.external.smart_image_view.SmartImageView;
import com.lanou.fooddemo.fragment.TopicsFragment;
import com.lanou.fooddemo.util.CommonUtil;

import java.net.URL;
import java.util.List;

/**
 * Created by dlios on 15-8-7.
 */
public class TopicAdapter extends BaseAdapter {


    private static final String LOG_TAG = "RecommeAdapter";//Log打印的标签
    private LayoutInflater inflater;   //布局注入器
    private List<DataBean> dataBeans;     //data目录下
    private Context context;


    //构造方法传参数
    public TopicAdapter(Context c, List<DataBean> beans) {
        context = c;
        inflater = LayoutInflater.from(c);
        dataBeans = beans;
    }


    @Override
    public int getCount() {
        if (CommonUtil.isListNotNull(dataBeans)) {
            return dataBeans.size() - 1;
        }
        return 0;
    }


    @Override
    public Object getItem(int position) {
        if (CommonUtil.isListNotNull(dataBeans)) {
            return dataBeans.get(position);
        }
        return null;
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (0 == position) {
            convertView = inflater.inflate(R.layout.topic_picturetopic, null);
            SmartImageView recommendedTopicsLeftImageView = (SmartImageView) convertView.findViewById(R.id.RecommendedTopicsLeftImageView);
            SmartImageView recommendeTopicsRightImageView = (SmartImageView) convertView.findViewById(R.id.RecommendeTopicsRightImageView);
            recommendedTopicsLeftImageView.setImageUrl(dataBeans.get(0).getCards().get(0).getPic().getUrl());
            recommendeTopicsRightImageView.setImageUrl(dataBeans.get(0).getCards().get(1).getPic().getUrl());
            recommendedTopicsLeftImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, TopicDetailsActivity.class);
                    String api = dataBeans.get(0).getCards().get(0).getDetail().getApi();
                    //将需要跳转的url携带信息传递到详细信息界面
                    intent.putExtra(TopicsFragment.TOPICDETAILS_KEY, api);
                    //跳转到详细信息界面
                    context.startActivity(intent);
                }
            });
            recommendeTopicsRightImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, TopicDetailsActivity.class);
                    String api = dataBeans.get(0).getCards().get(1).getDetail().getApi();
                    //将需要跳转的url携带信息传递到详细信息界面
                    intent.putExtra(TopicsFragment.TOPICDETAILS_KEY, api);
                    //跳转到详细信息界面
                    context.startActivity(intent);
                }
            });


        } else if (0 < position && 4 > position) {
            Holder holder = null;
            convertView = inflater.inflate(R.layout.topics_listview_item, null);
            holder = new Holder();
            holder.TopicHeadPortrait =
                    (SmartImageView) convertView.findViewById(R.id.TopicHeadPortrait);
            holder.topicSubject =
                    (TextView) convertView.findViewById(R.id.topicSubject);
            holder.topicNumber =
                    (TextView) convertView.findViewById(R.id.topicNumber);
            DataBean dataBean = null;
            //判断data是不是为空,如果不为空,就根据列表的数据顺序填充
            if (CommonUtil.isListNotNull(dataBeans)) {
                //dataBean = dataBeans.get(2);
                dataBean = dataBeans.get(position + 1);

            } else {
                Log.i(LOG_TAG, "databeans is null");
            }


            PicBean picBean = null;
            if (dataBean != null && dataBean.getType().equals(DataBean.TYPE_PIC_CARD)) {
                picBean = dataBean.getPic();


            } else {
                Log.i(LOG_TAG, "picBean is null");
            }


            if (picBean != null) {
                holder.TopicHeadPortrait.setImageUrl(picBean.getUrl());
                holder.topicSubject.setText(dataBean.getTitle());
                holder.topicNumber.setText(dataBean.getSubTitle());
            } else {
                Log.i(LOG_TAG, "picBean = null");
            }
        } else if (4 == position) {
            convertView = inflater.inflate(R.layout.topic_hottopic_item, null);
        } else if (4 < position) {
            Holder holder = null;
            convertView = inflater.inflate(R.layout.topics_listview_item, null);

            holder = new Holder();
            holder.TopicHeadPortrait =
                    (SmartImageView) convertView.findViewById(R.id.TopicHeadPortrait);
            holder.topicSubject =
                    (TextView) convertView.findViewById(R.id.topicSubject);
            holder.topicNumber =
                    (TextView) convertView.findViewById(R.id.topicNumber);

            DataBean dataBean = null;
            //判断data是不是为空,如果不为空,就根据列表的数据顺序填充
            if (CommonUtil.isListNotNull(dataBeans)) {
                dataBean = dataBeans.get(position + 1);
            } else {
                Log.i(LOG_TAG, "databeans is null");
            }


            PicBean picBean = null;
            if (dataBean != null && dataBean.getType().equals(DataBean.TYPE_PIC_CARD)) {
                picBean = dataBean.getPic();


            } else {
                Log.i(LOG_TAG, "picBean is null");
            }


            if (picBean != null) {
                holder.TopicHeadPortrait.setImageUrl(picBean.getUrl());
                holder.topicSubject.setText(dataBean.getTitle());
                holder.topicNumber.setText(dataBean.getSubTitle());
            } else {
                Log.i(LOG_TAG, "picBean = null");
            }
        }

        return convertView;
    }

    class Holder {
        SmartImageView TopicHeadPortrait;
        TextView topicSubject;
        TextView topicNumber;
    }
}


